var currentpEL = ''

function getLastMonthDay() {
    var year = document.getElementById("year").value;
    var month = document.getElementById("month").value;
    var day = new Date(year, month, 0);
    var lastdate = day.getDate();
    return lastdate;
}

function getNullDay() {
    var year = document.getElementById("year").value;
    var month = document.getElementById("month").value;
    var date = year + "-" + month + "-01";
    var myDate = new Date(date);
    var result = myDate.getDay();
    return result - 1; // return result
}

function init(day, dayFirst) {
    var ary = ["周日", "周一", "周二", "周三", "周四", "周五", "周六"];
    var count = 1;
    var totalTd = day + dayFirst;
    var daySize = 7;
    var totalTr = Math.ceil(totalTd / daySize);
    var table = document.createElement("table");
    table.id = "divCandler"
    var trHead = document.createElement("tr");
    for (var i = 0; i < ary.length; i++) {
        var thHead = document.createElement("th");
        thHead.innerHTML = ary[i];
        trHead.appendChild(thHead);
    }
    table.appendChild(trHead);
    for (var i = 1; i <= totalTr; i++) {
        var tr = document.createElement("tr");
        if (i == 1) {
            for (var j = 1; j <= dayFirst; j++) {
                var tdNull = document.createElement("td");
                tr.appendChild(tdNull);
            }
            for (var j = 1; j <= daySize - dayFirst; j++) {
                var td = document.createElement("td");
                td.innerHTML = count;
                td.appendChild(createContainer('09:00 - 12:00', '60', '10'))
                td.appendChild(createContainer('13:00 - 18:00', '120', '20'))
                tr.appendChild(td);
                count = count + 1;
            }
        } else {
            for (var j = 1; j <= daySize; j++) {
                if (count <= day) {
                    var td = document.createElement("td");
                    td.innerHTML = count;
                    td.appendChild(createContainer('09:00 - 12:00', '60', '10'))
                    td.appendChild(createContainer('13:00 - 18:00', '120', '20'))
                    tr.appendChild(td);
                    count = count + 1;
                }
            }
        }
        table.appendChild(tr);
    }
    var parentdiv = document.getElementById("container");
    var divCandler = document.getElementById("divCandler");
    if (null != divCandler) {
        parentdiv.removeChild(divCandler);
    }
    parentdiv.appendChild(table);
}

function getYear_Month() {
    var yearArr0 = [2021, 2020, 2019, 2018, 2017, 2016, 2015]
    var yearArr = ['2021年', '2020年', '2019年', '2018年', '2017年', '2016年', '2015年']
    var yearEl = document.getElementById('year')
    for (var i = 0; i < yearArr.length; i++) {
        var option = document.createElement("option");
        option.value = yearArr0[i]
        option.innerHTML = yearArr[i];
        yearEl.appendChild(option);
    }
    var osel = document.getElementById("year");
    var opts = osel.getElementsByTagName("option");
    opts[1].selected = true;
    var monthArr0 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
    var monthArr = ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月']
    var monthEl = document.getElementById('month')
    for (var i = 0; i < monthArr.length; i++) {
        var option = document.createElement("option");
        option.value = monthArr0[i]
        option.innerHTML = monthArr[i];
        monthEl.appendChild(option);
    }
}

function switchYear() {
    init(getLastMonthDay(), getNullDay());
}

function switchMonth() {
    init(getLastMonthDay(), getNullDay());
}

function setToday() {
    var myDate = new Date();
    var nowDate = myDate.getDate();
    var nowMonth = myDate.getMonth() + 1;
    var yearEl = document.getElementById('year')
    var monthEl = document.getElementById('month')
    var monthIndex = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12].findIndex((item, index) => {
        return item == nowMonth ? index : ''
    })
    yearEl.options[0].selected = true;
    monthEl.options[monthIndex].selected = true;
    init(getLastMonthDay(), getNullDay() + 1);
    var currentTd = getNullDay() + nowDate
    var tds = document.getElementsByTagName('td')
    tds[currentTd].style.border = '1px solid green';
    //tds[currentTd].style.color = 'red'
    for (var i = currentTd + 1; i < tds.length; i++) {
        var span = document.createElement("span")
        span.innerHTML = '未开放'
        span.style.color = 'red'
        tds[i].innerHTML = tds[i].innerText.slice(0, 2)
        tds[i].appendChild(span)
    }
}

function reset() {
    var yearEl = document.getElementById('year')
    var monthEl = document.getElementById('month')
    yearEl.options[1].selected = true;
    monthEl.options[0].selected = true;
    init(getLastMonthDay(), getNullDay());
}

function createContainer(time, total, current) {
    var pEL = document.createElement("p");
    pEL.statu = false
    pEL.classList.add('pEL')
    pEL.innerHTML = time
    var pEL1 = document.createElement("p");
    pEL1.classList.add('pEL1')
    pEL.appendChild(pEL1)
    var pEL2 = document.createElement("p");
    pEL2.innerHTML = total;
    pEL.appendChild(pEL2)
    var pEL3 = document.createElement("p");
    pEL3.classList.add('pEL1')
    pEL3.style.backgroundColor = 'orange';
    pEL.appendChild(pEL3)
    var pEL4 = document.createElement("p");
    pEL4.innerHTML = current;
    pEL.appendChild(pEL4)
    pEL.onclick = function() {
        document.getElementById('textarea').value = ''
            // 能否连续预约同一个场次
            // if (this.statu == false) {
            //     document.getElementById("success").style.display = 'block';
            //     document.getElementById("fail").style.display = 'none';
            // } else {
            //     document.getElementById("success").style.display = 'none';
            //     document.getElementById("fail").style.display = 'block';
            // }

        currentpEL = this
        setFloat('block')
        document.getElementById('textarea').value = currentpEL.title
        var year = document.getElementById("year").value;
        var month = document.getElementById("month").value;
        var riQi = document.getElementById("riQi");
        var changCi = document.getElementById("changCi");
        var KYY = document.getElementById("KYY");
        var YYY = document.getElementById("YYY");
        riQi.innerHTML = year + '年' + month + '月';
        var text = this.parentNode.innerText
        riQi.innerHTML = year + '年' + month + '月' + text.slice(0, 2) + '日';
        changCi.innerHTML = this.innerText.slice(0, 13);
        if (this.innerText.slice(0, 13) == '09:00 - 12:00') {
            KYY.innerHTML = this.innerText.slice(13, 17);
            YYY.innerHTML = this.innerText.slice(17, 22);
        } else {
            KYY.innerHTML = this.innerText.slice(13, 18);
            YYY.innerHTML = this.innerText.slice(18, 23);
        }
    }
    return pEL
}

function setFloat(statu) {
    var divEL = document.getElementById("divEL")
    divEL.style.display = statu;
}
var cls = document.getElementById("clear");
var success = document.getElementById("success");
var fail = document.getElementById("fail");

cls.onclick = function() {
    setFloat('none')
}
success.onclick = function() {
    currentpEL.statu = true
    currentpEL.style.backgroundColor = 'rgb(146, 223, 233)';
    currentpEL.lastElementChild.innerText = Number(currentpEL.lastElementChild.innerText) + 1;
    currentpEL.title = document.getElementById('textarea').value
    alert('您已预约成功！！！');
    setFloat('none');
}
fail.onclick = function() {
    currentpEL.statu = false
    currentpEL.style.backgroundColor = 'white';
    currentpEL.title = document.getElementById('textarea').value = ''
    currentpEL.lastElementChild.innerText = Number(currentpEL.lastElementChild.innerText) - 1;
    alert('取消当前预约！！！');
    setFloat('none')
}